from flask import Blueprint, render_template, request, make_response
from flask_jwt_extended import set_access_cookies, create_access_token

from com.gen_captcha import get_captcha_code_and_content
from extensions import redis_db
from orms import CategoryORM, UserORM

admin_bp = Blueprint('admin', __name__)


@admin_bp.get('/admin/')
def admin_index():
    return render_template('admin/index.html')


@admin_bp.get('/admin/console1/')
def admin_console1():
    return render_template('admin/console1.html')


@admin_bp.get('/admin/user/')
def admin_user():
    return render_template('admin/user.html')


@admin_bp.get('/admin/add_user/')
def add_user():
    return render_template('admin/user_add.html')


@admin_bp.get('/admin/edit_user/')
def admin_user_edit():
    return render_template('admin/edit.html')


@admin_bp.get('/admin/cate_add/')
def admin_cate_add():
    return render_template('admin/cate_add.html')


@admin_bp.get('/admin/cate_edit/')
def admin_cate_edit():
    return render_template('admin/cate_edit.html')


@admin_bp.get('/admin/article/')
def admin_article():
    return render_template('admin/article_list.html')


@admin_bp.get('/admin/article_preview/')
def admin_article_preview():
    return render_template('admin/article_preview.html')


@admin_bp.get('/admin/cate/')
def get_cate_list():
    cate_list: list[CategoryORM] = CategoryORM.query.all()
    # 数据量大就需要分页
    return {
        'code': 0,
        'message': '获取分类数据成功',
        'data': [{
            'id': cate.id,
            'name': cate.name,
        } for cate in cate_list]
    }


@admin_bp.get('/admin/login/')
def admin_login():
    return render_template('admin/login.html')


@admin_bp.post('/admin/login/')
def admin_login_post():
    data = request.json
    # 校验图片验证码
    image_code_uuid = data['image_code_uuid']
    captcha_code_redis = redis_db.get_chapter_image(image_code_uuid)
    captcha = data['captcha']
    print('----------------------------------------------------')
    print("|captcha:", captcha)
    print("|captcha_code_redis:", captcha_code_redis)
    print("|result:", captcha_code_redis == captcha)
    print('----------------------------------------------------')
    if captcha != captcha_code_redis:
        return {
            'code': -1,
            'message': '图片验证码错误'
        }
    mobile = data['mobile']
    user: UserORM = UserORM.query.filter(UserORM.mobile == mobile).first()
    if not user:
        return {
            'code': -1,
            'message': '用户不存在，请先注册'
        }
    pwd = data.get('password')
    if not user.check_passwd(pwd):
        return {
            'code': -1,
            'message': '用户密码错误'
        }
    if user.is_admin != 1:
        return {
            'code': -1,
            'message': '您不是管理员'
        }
    # 全部校验成功，发布token
    access_token = create_access_token(user)
    _next = request.args.get('next')

    response = make_response({
        'code': 0,
        'message': '登陆成功',
        'next': _next if _next else '/admin/',
        'access_token': access_token
    })
    set_access_cookies(response, access_token)
    return response


@admin_bp.get('/admin/get_captcha/')
def get_captcha():
    image_code_uuid = request.args.get('image_code_uuid')
    # 需要将图片的唯一标识记录到Redis数据库
    text, image = get_captcha_code_and_content()
    # 生成响应体对象
    response = make_response(image)
    # 设置response格式为图片
    response.content_type = 'image/png'
    #
    redis_db.store_chapter_image(image_code_uuid, text)
    return response
